package com.suzhe.ymall.promotion.config;

import com.suzhe.ymall.common.annotation.EnableExceptionHandler;
import com.suzhe.ymall.common.annotation.EnableHttpAuth;
import com.suzhe.ymall.common.annotation.EnableMvc;
import com.suzhe.ymall.common.annotation.EnableRedis;
import com.suzhe.ymall.common.enums.ZkLockPathEnum;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.apache.curator.retry.RetryNTimes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableExceptionHandler //开启统一异常处理
@EnableRedis //开启redis
@EnableMvc
@EnableHttpAuth
public class PromotionConfig {

    @Autowired
    ZkCuratorProperties zkCuratorProperties;

    @Bean(initMethod = "start")
    public CuratorFramework curatorFramework() {
        CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient(
                zkCuratorProperties.getConnectString(),
                zkCuratorProperties.getSessionTimeoutMs(),
                zkCuratorProperties.getConnectionTimeoutMs(),
                new RetryNTimes(zkCuratorProperties.getRetryCount(),//
                        zkCuratorProperties.getElapsedTimeMs()));
        return curatorFramework;
    }

    @Bean(name = "userCouponLock")
    public InterProcessMutex userCouponLock(CuratorFramework curatorFramework){

        return new InterProcessMutex(curatorFramework, ZkLockPathEnum.USER_COUPON.value());
    }

    @Bean(name = "couponLock")
    public InterProcessMutex couponLock(CuratorFramework curatorFramework){

        return new InterProcessMutex(curatorFramework, ZkLockPathEnum.COUPON.value());
    }


}
